package zsw.reflect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.util.ClassUtils;


public class TestJdkAnnotation implements InvocationHandler {
	
	
	private  Object target;
	
	private Object p;
	
	public Object getProxy(Object obj) {
		this.target=obj;
	    Object o =Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), this);
	    this.p=o;
		return o;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		//proxy---》代理类   ,method---->接口的方法
		
		
		Method method2 = target.getClass().getMethod(method.getName(), method.getParameterTypes());
		Method methods = ClassUtils.getMostSpecificMethod(method,target.getClass());
		
		System.out.println(method2==methods);
		
		System.out.println(method2.getDeclaringClass());
		System.out.println(methods.getDeclaringClass());
		
		
		HasPrimesission annotation = methods.getAnnotation(HasPrimesission.class);
		
		String value="";
		Object invoke=null;
		if(annotation!=null) {
			value=annotation.value();
		}
		System.out.println("是否是代理对象"+Proxy.isProxyClass(proxy.getClass()));
		System.out.println("是否是代理对象名字："+method.getDeclaringClass());
		System.out.println("是否实现类对象名字："+methods.getDeclaringClass());
		System.out.println("是否是代理对象名字："+(p==proxy));
		if("yes".equalsIgnoreCase(value)) {
			System.out.println("开始代理");
			invoke = method.invoke(target, args);
			System.out.println("结束代理");
			
		}else {
			 invoke = method.invoke(target, args);
		}
		
		return invoke;
	}

	public static void main(String[] args) {
		TestJdkAnnotation an=new TestJdkAnnotation();
		TestAnnotation2 d=new TestAnnotation2();
		TestAnnotationInterface proxy = (TestAnnotationInterface) an.getProxy(d);
		//proxy.say("zsw");
		proxy.hasPremission("ff");
		
	}
}
